package org.example;

import java.util.ArrayList;
import java.util.List;

public class Test2 {
    //lettcode 组合总数III https://leetcode.cn/problems/combination-sum-iii/description/
    public List<List<Integer>> ret;
    public List<Integer> list;
    public int sum;
    public List<List<Integer>> combinationSum3(int k, int n) {
        ret = new ArrayList<>();
        list = new ArrayList<>();
        backtracking(1,k,n);
        return ret;
    }

    public void backtracking(int start, int k, int n) {
        if (list.size() == k) {
            if (sum == n) {
                ret.add(new ArrayList<>(list));
            }
            return;
        }
        for (int i = start; i <= 9; i++) {
            sum += i;
            list.add(i);
            backtracking(i + 1,k,n);
            sum -= i;
            list.remove(list.size() - 1);
        }
    }
}
